import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.scene.control.Button;

public class Main extends Application{

    //创建初始值分别为为"0","0","?","0"的StringBuilder对象s1,s2,s3,s4
    StringBuilder s1 = new StringBuilder("0");
    StringBuilder s2 = new StringBuilder("0");
    StringBuilder s3 = new StringBuilder("?");
    StringBuilder s4 = new StringBuilder("0");

    // 初始化Label对象
    Label display1 = new Label(s1.toString());
    Label display2 = new Label(s2.toString());
    Label display3 = new Label(s3.toString());
    Label display4 = new Label(s4.toString());

    // 将target_display设置为display2
    Label target_display = display2;

    // 将s_target设置为s2
    StringBuilder s_target = s2;


    public void start(Stage primarystage){
        VBox vbox = new VBox();
        // 设置垂直布局的间距
        vbox.setSpacing(50);
        // 设置布局中子节点的对齐方式为居中
        vbox.setAlignment(Pos.CENTER);

        // 创建主菜单场景，使用垂直布局作为根节点
        Scene menuscene = new Scene(vbox, 400, 600);

        // 设置主舞台标题、场景和显示
        primarystage.setTitle("分数计算器");
        primarystage.setScene(menuscene);
        primarystage.show();

        // 创建三个主菜单按钮
        Button menubutton1 = new Button("四则运算及其幂运算");
        menubutton1.setPrefSize(150, 50);
        Button menubutton2 = new Button("逻辑运算");
        menubutton2.setPrefSize(150, 50);
        Button menubutton3 = new Button("退出程序");
        menubutton3.setPrefSize(150, 50);

        // 将三个按钮添加到垂直布局中
        vbox.getChildren().addAll(menubutton1, menubutton2, menubutton3);

        // 第一个主菜单按钮的事件处理
        menubutton1.setOnAction(e -> {

            // 创建网格布局
            GridPane gridPane = new GridPane();
            gridPane.setHgap(5);  // 设置网格水平间隔
            gridPane.setVgap(5);  // 设置网格垂直间隔

            // 创建场景，将网格布局设置为根节点，设置场景的宽度和高度
            Scene scene = new Scene(gridPane, 400, 600);

            // 设置主舞台标题和场景
            primarystage.setTitle("四则运算及其幂运算");
            primarystage.setScene(scene);

            // 创建显示结果的标签，设置其大小、字体和对齐方式
            display1.setPrefSize(400, 100);
            display1.setFont(Font.font(25));
            display1.setStyle("-fx-alignment: CENTER-RIGHT;");

            // 将显示标签添加到网格布局中，占据4列和1行
            gridPane.add(display1, 0, 0, 4, 1);

            // 定义按钮的标签数组，包括运算符、数字和其他功能按钮
            String[] buttonLabels = {
                    "AC", "⌫", "^", "=",
                    "7", "8", "9", "+",
                    "4", "5", "6", "-",
                    "1", "2", "3", "×",
                    "/", "0", ".", "÷"
            };

            // 初始化按钮布局的行和列变量
            int row = 2;
            int col = 0;

            // 创建返回按钮，用于返回到主菜单
            Button backButton = new Button("back");
            GridPane.setValignment(backButton, VPos.TOP);
            GridPane.setHalignment(backButton, HPos.LEFT);
            gridPane.add(backButton, 0, 0);

            // 设置返回按钮的事件处理，返回到主菜单并重置相关变量
            backButton.setOnAction(e1 -> {
                primarystage.setScene(menuscene);
                s1.setLength(0);
                s1.append("0");
                display1.setText(s1.toString());
            });

            // 遍历按钮标签数组，创建并添加按钮到网格布局中，并设置相应的事件处理
            for (String label : buttonLabels) {
                Button button = new Button(label);
                button.setPrefSize(95, 95);
                button.setFont(Font.font(25));
                gridPane.add(button, col, row);

                // 按钮的事件处理
                if (row > 2)
                    button.setOnAction(e1 -> copyButtonClick(label, s1, display1));
                else if (row == 2 && col == 0)
                    button.setOnAction(actionEvent -> {
                        s1.setLength(0);
                        s1.append("0");
                        display1.setText(s1.toString());
                    });
                else if (row == 2 && col == 1)
                    button.setOnAction(actionEvent -> {
                        if (!s1.isEmpty())
                            s1.deleteCharAt(s1.length() - 1);
                        if (s1.isEmpty())
                            s1.append("0");
                        display1.setText(s1.toString());
                    });
                else if (row == 2 && col == 2)
                    button.setOnAction(e1 -> copyButtonClick(label, s1, display1));
                else if (row == 2 && col == 3)
                    button.setOnAction(actionEvent -> display1.setText(Calculate.calculate(s1.toString())));

                // 更新列变量，超过3时返回0，并增加行变量
                col++;
                if (col > 3) {
                    col = 0;
                    row++;
                }
            }
        });

        // 第二个主菜单按钮的事件处理
        menubutton2.setOnAction(e -> {
            // 创建网格布局
            GridPane gridPane = new GridPane();
            gridPane.setHgap(5);
            gridPane.setVgap(5);

            // 创建场景
            Scene scene = new Scene(gridPane, 400, 600);

            // 设置主舞台标题和场景
            primarystage.setTitle("逻辑运算");
            primarystage.setScene(scene);
            primarystage.show();

            // 创建用于显示操作数和结果的标签
            display2.setPrefSize(180, 200);
            display2.setFont(Font.font(25));
            display2.setStyle("-fx-alignment: CENTER;");

            display3.setPrefSize(40, 200);
            display3.setFont(Font.font(25));

            display4.setPrefSize(180, 200);
            display4.setFont(Font.font(25));

            // 将标签添加到网格布局中
            gridPane.add(display2, 0, 0, 2, 1);
            gridPane.add(display3, 2, 0, 2, 1);
            gridPane.add(display4, 3, 0, 2, 1);

            // 定义按钮标签数组，包括数字、运算符和其他功能按钮
            String[] buttonLabels = {
                    "7", "8", "9", "AC",
                    "4", "5", "6", "⌫",
                    "1", "2", "3", "next",
                    "/", "0", ".", "OK"
            };

            // 初始化按钮布局的行和列变量
            int row = 2;
            int col = 0;

            // 创建返回按钮
            Button backButton = new Button("back");
            GridPane.setValignment(backButton, VPos.TOP);
            GridPane.setHalignment(backButton, HPos.LEFT);
            gridPane.add(backButton, 0, 0);

            // 设置返回按钮的事件处理，返回到主菜单并重置相关变量
            backButton.setOnAction(e1 -> {
                primarystage.setScene(menuscene);
                s2.setLength(0);
                s2.append("0");
                s3.setLength(0);
                s3.append("?");
                s4.setLength(0);
                s4.append("0");
                display2.setText(s2.toString());
                display3.setText(s3.toString());
                display4.setText(s4.toString());
            });

            // 遍历按钮标签数组，创建并添加按钮到网格布局中，并设置相应的事件处理
            for (String label : buttonLabels) {
                Button button = new Button(label);
                button.setPrefSize(95, 95);
                button.setFont(Font.font(25));
                gridPane.add(button, col, row);

                // 按钮的事件处理
                if (col < 3)
                    button.setOnAction(e1 -> copyButtonClick(label, s_target, target_display));
                else if (row == 2)
                    button.setOnAction(actionEvent -> {
                        s_target.setLength(0);
                        s_target.append("0");
                        target_display.setText(s_target.toString());
                    });
                else if (row == 3)
                    button.setOnAction(actionEvent -> {
                        if (!s_target.isEmpty())
                            s_target.deleteCharAt(s_target.length() - 1);
                        if (s_target.isEmpty())
                            s_target.append("0");
                        target_display.setText(s_target.toString());
                    });
                else if (row == 4)
                    button.setOnAction(e1 -> changeTarget());
                else if (row == 5)
                    button.setOnAction(actionEvent -> {
                        switch (new Fraction().compare(Fraction.parseFraction(s2.toString()),
                                Fraction.parseFraction(s4.toString()))) {
                            case 1:
                                display3.setText(">");
                                break;
                            case 2:
                                display3.setText("=");
                                break;
                            case 3:
                                display3.setText("<");
                        }
                    });

                // 更新列变量，超过3时返回0，并增加行变量
                col++;
                if (col > 3) {
                    col = 0;
                    row++;
                }
            }
        });

        // 第三个主菜单按钮的事件处理，退出程序
        menubutton3.setOnAction(actionEvent -> System.exit(0));
    }


    // 将s_target和target_display更改为s4和display4
    private void changeTarget() {
        s_target = s4;
        target_display = display4;
    }


    // 当按钮被点击时，将按钮的标签内容添加到字符串构建器中，并更新显示标签的文本
    private void copyButtonClick(String label, StringBuilder s, Label display) {
        if (s.toString().equals("0")) {
            s.setLength(0);
        }
        s.append(label);
        display.setText(s.toString());
    }

}